<template>
  <el-dialog title="添加机构" :visible.sync="dialogFormVisible" top="20vh">
    <el-form ref="formInline" :inline="true" :model="formInline" :rules="rules" class="form-class" label-position="right" size="small" label-width="100px">
      <el-row>
        <el-col :span="12">
      <el-form-item label="上级单位:" prop="parent" >
        <el-select class="main-select-tree" ref="selectTree" clearable v-model="formInline.parent" style="width: 130%;" placeholder="请选择上级单位">
          <el-option v-for="item in formatData(deptData)" :key="item.value" :label="item.label" :value="item.value"
                     style="display: none;"/>
          <el-tree class="main-select-el-tree" ref="selecteltree"
                   :data="deptData"
                   node-key="id"
                   highlight-current
                   :props="defaultProps"
                   @node-click="handleNodeClick"
                   :current-node-key="this.formInline.parent"
                   :expand-on-click-node="expandOnClickNode"
                   :default-expand-all='false'/>
        </el-select>
      </el-form-item>
        </el-col>
        <el-col :span="12">
      <el-form-item label="部门名称:" prop="name" >
        <el-input v-model="formInline.name" :style="{width: '140%'}" placeholder="请输入部门名称"></el-input>
      </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
      <el-form-item label="负责人:" prop="owner">
        <el-input v-model="formInline.owner" :style="{width: '140%'}" placeholder="请输入负责人姓名"></el-input>
      </el-form-item>
        </el-col>
        <el-col :span="12">
      <el-form-item label="排序:" prop="sort">
        <el-input-number size="medium" :min="0"  :max="999" v-model="formInline.sort"></el-input-number>
      </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
      <el-form-item label="电话:" prop="phone">
        <el-input v-model="formInline.phone" :style="{width: '140%'}" placeholder="请输入负责人电话"></el-input>
      </el-form-item>
        </el-col>
        <el-col :span="12">
      <el-form-item label="邮箱:" prop="email">
        <el-input v-model="formInline.email" :style="{width: '140%'}" placeholder="请输入负责人邮箱"></el-input>
      </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm('formInline')">确 定</el-button>
      <el-button @click="resetForm('formInline')">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addDeptData } from '@/api/dept'
  import { successMsg } from '@/utils/tools'
  export default {
    name: 'Insert',
    props:{ /*----接收数据----*/
      deptData:{
        type:Array,
        required:true
      }
    },
    data() {
      /*-自定义对下拉树进行校验*/
      var validatorPhone = function (rule, value, callback) {
        if (value === '') {
          callback(new Error('手机号不能为空'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        dialogFormVisible: false,
        expandOnClickNode: true,
        options:[],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        formInline: {
          parent: '',
          name: '',
          owner:'',
          phone:'',
          email:'',
          sort:0
        },
        rules:{
          name:[
            { required: true, message: '请输入部门名称', trigger: 'blur' },
          ]
        }
      };
    },
    methods:{
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid){
            addDeptData(this.formInline).then(res=>{
              if(res.code===2000) {
                successMsg(res.msg)
                this.$refs[formName].resetFields();
                this.$emit('reloadTable')
                this.dialogFormVisible = false
              }
            })
          }else{
            return false;
          }
        });
      },
      resetForm(formName){
        this.$refs[formName].resetFields();
        this.dialogFormVisible = false
      },
      // 四级菜单
      formatData(data){
        let options = [];
        data.forEach((item,key) => {
          options.push({label:item.name,value:item.id});
          if(item.children){
            item.children.forEach((items,keys) => {
              options.push({label:items.name,value:items.id});
              if(items.children){
                items.children.forEach((itemss,keyss) => {
                  options.push({label:itemss.name,value:itemss.id});
                  if(itemss.children){
                    itemss.children.forEach((itemsss,keysss) => {
                      options.push({label:itemsss.name,value:itemsss.id});
                    });
                  }
                });
              }
            });
          }
        });
        return options;
      },
      handleNodeClick(node){
        this.formInline.parent = node.id;
        this.$refs.selectTree.blur();
      }
    }
  }
</script>

<style>
  .main-select-el-tree .el-tree-node .is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .main-select-el-tree .el-tree-node.is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .label_padding1{
    padding-left: 50px;
  }
  .label_padding2{
    padding-left: 65px;
  }
  .input_width1{
    width: 150%;
  }
  .input_width2{
    width: 145%;
  }
</style>
